﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tunnel.Tools;

namespace Tunnel.Plan
{
    public partial class PlanTime : Form
    {
        public PlanTime()
        {
            InitializeComponent();
        }
        DataTable dtTimePlan;
        DataTable dtPlan;
        private async void LoadTheTimePlan()
        {
            string sql = "SELECT M_TimePlan.TimePlanID,M_TimePlan.Hour,M_TimePlan.Minute,M_TimePlan.PlanID,M_Plan.PlanName,M_Plan.PlanText FROM M_TimePlan LEFT OUTER JOIN M_Plan ON M_TimePlan.PlanID = M_Plan.PlanID ORDER BY Hour,Minute";
            DBHelper dh = new DBHelper();
            dtTimePlan = await dh.GetTableAsync(sql);
            dataGridView1.DataSource = dtTimePlan;
        }
        private async void LoadThePlan()
        {
            string sql = "SELECT PlanID,PlanName FROM M_Plan";
            DBHelper dh = new DBHelper();
            dtPlan = await dh.GetTableAsync(sql);
            cbPlanList.DataSource = dtPlan;
        }

        private void PlanTime_Load(object sender, EventArgs e)
        {
            LoadTheTimePlan();
            LoadThePlan();
            cbHour.Text = DateTime.Now.Hour.ToString();
            cbMinute.Text = DateTime.Now.Minute.ToString();
        }

        private async void BtAddTimePlan_Click(object sender, EventArgs e)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("确定增加该定时计划么\r\n每日");
            stringBuilder.Append(cbHour.Text);
            stringBuilder.Append("时");
            stringBuilder.Append(cbMinute.Text);
            stringBuilder.Append("分，运行预案[");
            stringBuilder.Append(cbPlanList.Text);
            stringBuilder.Append("]");
            DialogResult dialogResult = MessageBox.Show(stringBuilder.ToString(),"确认增加定时计划",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
            if (dialogResult == DialogResult.No)
                return;
            DBHelper dh = new DBHelper();
            SqlCommand sqlCommand = new SqlCommand("INSERT INTO [dbo].[M_TimePlan] ([TimePlanID],[Hour],[Minute],[PlanID]) VALUES (@id,@hour,@minute,@planid)");
            if (string.IsNullOrEmpty(cbHour.Text))
                return;
            if (string.IsNullOrEmpty(cbMinute.Text))
                return;
            if (string.IsNullOrEmpty(cbPlanList.SelectedValue.ToString()))
                return;
            sqlCommand.Parameters.Add(new SqlParameter("@id", DBHelper.GetGuid));
            sqlCommand.Parameters.Add(new SqlParameter("@hour", cbHour.Text));
            sqlCommand.Parameters.Add(new SqlParameter("@minute", cbMinute.Text));
            sqlCommand.Parameters.Add(new SqlParameter("@planid", cbPlanList.SelectedValue.ToString()));
            int result = await dh.RunCommandAsync(sqlCommand);
            LoadTheTimePlan();
            if (result != 0)
            {
                MessageBox.Show("添加预案定时成功");
            }
            Program.MainForm.timeToRun.RestartTimer();
        }

        private async void BtDeleteTimePlan_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("确定删除该定时计划吗？", "删除计划确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (dialogResult == DialogResult.No)
                return;
            DBHelper dh = new DBHelper();
            string deleteid = dataGridView1.SelectedRows[0].Cells["TimePlanID"].Value.ToString();
            if (string.IsNullOrEmpty(deleteid))
                return;
            SqlCommand sqlCommand = new SqlCommand("DELETE FROM [dbo].[M_TimePlan] WHERE TimePlanID=@id");
            sqlCommand.Parameters.Add(new SqlParameter("@id", deleteid));            
            int result = await dh.RunCommandAsync(sqlCommand);
            LoadTheTimePlan();
            if (result != 0)
            {
                MessageBox.Show("删除预案定时成功");
            }
            Program.MainForm.timeToRun.RestartTimer();
        }
    }
}
